﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ToolUtity;

namespace AHCS
{
    public partial class RegForm : Form
    {
        public RegForm()
        {
            InitializeComponent();
        }

        //激活按钮
        private void button1_Click(object sender, EventArgs e)
        {
           
            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                try
                {
                    string regcoede = textBox2.Text.Trim();
                    //先解密第二个
                    string minwen = Encryption.DisEncryPW(regcoede);
                    if (!string.IsNullOrEmpty(minwen))
                    {
                        int i = minwen.IndexOf("ttt");
                        if (i == -1)
                        {
                            MessageBox.Show("注册码错误！");
                            return;
                        }

                        
                        string ts = minwen.Substring(0, i);
                        string dcode = minwen.Substring(i+3);
                        dcode = dcode.Substring(0, dcode.Length - 14);//YYYYMMddHHmmss
                        if (textBox1.Text == dcode)
                        {
                            List<string> listreg= TimeClass.ReadSettingAll();
                            if (listreg.Contains(regcoede))
                            {
                                MessageBox.Show("注册码已使用无效，请购买新注册码");
                                return;
                            }

                            int addday = ts.ToInt32();//本次激活增加天数
                            string saveDay= TimeClass.ReadSetting("saveDay","");
                            string writedt = "";
                            if (saveDay=="")
                            {
                                writedt = DateTime.Now.AddDays(addday).ToString("yyyy-MM-dd");
                            }
                            else
                            {
                               string dqrq=Encryption.DisEncryPW(saveDay);
                                DateTime dtdq = DateTime.Parse(dqrq);
                                DateTime xdqr=  dtdq.AddDays(addday);
                                writedt= xdqr.ToString("yyyy-MM-dd");
                            }
                             
                            string str = Encryption.EncryPW(writedt);
                            TimeClass.WriteSetting("saveDay", str);// 保存到期日

                            //保存本次的激活码
                            string strreg = "reg"+DateTime.Now.ToString("yyyyMMddhhmmss");
                            TimeClass.WriteSetting(strreg, regcoede);// 保存本次激活码
                            MessageBox.Show("恭喜！，激活增加" + ts+"天,请重新启动");
                         //  lblday.Text = "软件剩余使用：" + ts + "天";
                           Environment.Exit(0);
                          
                        }
                        else
                        {
                            MessageBox.Show("注册码错误！");
                            this.Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show("注册码错误！");
                    }
                }
                catch (Exception)
                {

                    MessageBox.Show("注册码错误！");
                }
              

                

              
            }

          
        }

        private void RegForm_Load(object sender, EventArgs e)
        {
            textBox1.Text = TimeClass.GetCpuId();
            if (Common.saveday<0)
            {
                lblday.Text = "软件已过使用期，请联系安徽长升信息技术有限公司 (QQ:878163778;微信：19810985732)购买";
            }
            else
            {
                lblday.Text = "软件剩余使用：" + Common.saveday + "天";
            }



        }

        private void btncopy_Click(object sender, EventArgs e)
        {
            if (textBox1.Text!="" )
            {
                string ss = textBox1.Text;
                if (txttjm.Text != "")
                {
                    ss+= $"+{txttjm.Text}";
                }
                Clipboard.SetDataObject(ss);
            }
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            ISkeyong();

        }

        public  void ISkeyong()
        {
            
            if (Common.ISkeyong())
            {
                lblday.Text = "软件剩余使用：" + Common.saveday + "天";
            }
            else
            {
                lblday.Text = "软件已过使用期，请联系安徽长升信息技术有限公司 (QQ:878163778;微信：19810985732)购买";
                timer1.Enabled = false;
               
                //MessageBox.Show("软件已过使用期，请联系公司购买");
                //Environment.Exit(0);
            }
        }
    }
}
